0426_解決過去沒辦法多個空格使用傾聽者機制的問題
後面試著讓資料可以寫進資料庫並建出資料
package MyFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.TextArea;
import java.awt.event.TextEvent;
//0831知道如何使用傾聽者及自動作加減,還有能自己創造出格子出來
class 實驗二 extends JFrame{
// 此視窗所用到的元件
int fieldWidth = 10;
JPanel panel, inPanel, outPanel;
JLabel inputFieldLabel, outputFieldLabel;
JTextField inputField, outputField,inputField2,outputField2;
JButton inputButton,inputButton2;
public 實驗二() {
super("視窗事件範例");//為什麼子類別可以繼承這個範例?這範例從哪來的?
initApp();
}
// 元件相關設置
public void initApp() {
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//BoxLayout是讓物件保持水平或垂直平行對齊的語法
inPanel = new JPanel();
outPanel = new JPanel();
inputFieldLabel = new JLabel("輸入文字");
inputField = new JTextField("輸入1");
inputField.addActionListener(new inputFieldActionListener()); // 要將事件處理加入
inputField2 = new JTextField("輸入2");
inputField2.addActionListener(new inputFieldActionListener());
inputButton = new JButton("確定");
inputButton.addActionListener(new inputButtonActionListener()); // 要將事件處理加入
inputButton2 = new JButton("確定");
inputButton2.addActionListener(new inputButtonActionListener()); // 要將事件處理加入
outputFieldLabel = new JLabel("輸出文字");
outputField = new JTextField("輸出1");
outputField2 = new JTextField("輸出2");
panel.add(inputFieldLabel);
panel.add(inputField);
panel.add(inputField2);
panel.add(outputFieldLabel);
inPanel.add(inputButton);
outPanel.add(outputFieldLabel);
outPanel.add(outputField);
outPanel.add(outputField2);
panel.add(inPanel);
panel.add(outPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.getContentPane().add(panel);
this.setVisible(true);
}
// inputField的事件處理
private class inputFieldActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
changeOutputText();
}
}
// inputButton的事件處理
private class inputButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
changeOutputText();
}
}
private class InputFieldStr2ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e){
changeOutputText();
}
}
private class inputButton2ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
changeOutputText();
}
}
public void changeOutputText() {
String input_field_str = inputField.getText();
outputField.setText(input_field_str);
String InputFieldStr2 = inputField2.getText();
outputField2.setText(InputFieldStr2);
}
public static void main(String[] args) {
實驗二 app = new 實驗二();
}
}